package com.ey12.service;

import java.util.ArrayList;
import java.util.List;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import com.ey12.db.dao.GmCserverDAO;
import com.ey12.db.dao.GmGameDAO;
import com.ey12.db.dao.GmGameStoreDAO;
import com.ey12.db.dao.GmOrderDAO;
import com.ey12.db.dao.GmUserDAO;
import com.ey12.db.domain.GmCserver;
import com.ey12.db.domain.GmGame;
import com.ey12.db.domain.GmGameStore;
import com.ey12.db.domain.GmOrder;
import com.ey12.db.domain.GmUser;
import com.ey12.file.DBPoolFactory;
import com.ey12.utils.DateUtil;

public class GmOrderService {

	private GmOrderDAO gmOrderDAO;
	private GmUserDAO gmUserDAO;
	private GmGameDAO gmGameDAO;
	private GmCserverDAO gmCserverDAO;
	private GmGameStoreDAO gmGameStoreDAO;

	public GmUserDAO getGmUserDAO() {
		return gmUserDAO;
	}

	public void setGmUserDAO(GmUserDAO gmUserDAO) {
		this.gmUserDAO = gmUserDAO;
	}

	public GmOrderDAO getGmOrderDAO() {
		return gmOrderDAO;
	}

	public void setGmOrderDAO(GmOrderDAO gmOrderDAO) {
		this.gmOrderDAO = gmOrderDAO;
	}

	private int getPercents(double a, double b) {
		NumberFormat nt = NumberFormat.getPercentInstance();
		nt.setMinimumFractionDigits(0);
		double y = (a + 1) / b;
		String result = nt.format(y);
		System.out.println(result);
		if (result.indexOf("%") != -1) {
			result = result.replaceAll("%", "");
		}
		return Integer.parseInt(result);
	}

	// public static void main(String[] arg){
	// System.out.println(new GmOrderService().getPercents(3, 10));
	// }

	public int findBuyRate(GmUser guser) {
		DecimalFormat df = new DecimalFormat("#.##");

		System.out.println("findBuyRate-using");
		List list1 = gmOrderDAO.findBuyNum1(guser);
		Long num1 = (Long) list1.get(0);
		System.out.println("findBuyRate-num1=" + num1);
		List list2 = gmOrderDAO.findBuyNum2(guser);
		Long num2 = (Long) list2.get(0);
		System.out.println("findBuyRate-num2=" + num2);
		int rerult = getPercents(num1.doubleValue(), num2.doubleValue());
		System.out.println("findBuyRate-num=" + rerult);
		return rerult;
	}

	public int findSaleRate(GmUser guser) {
		System.out.println("findSaleRate-using");
		List list1 = gmOrderDAO.findSaleNum1(guser);
		Long num1 = (Long) list1.get(0);
		System.out.println("findSaleRate-num1=" + num1);
		List list2 = gmOrderDAO.findSaleNum2(guser);
		Long num2 = (Long) list2.get(0);
		System.out.println("findSaleRate-num2=" + num2);
		int rerult = getPercents(num1.doubleValue(), num2.doubleValue());
		System.out.println("findSaleRate-num=" + rerult);
		return rerult;
	}

	public List getMoneyBackDetial(GmUser guser) {
		List list = gmOrderDAO.findMoneyBackDetial(guser);
		System.out.println("getMoneyBackDetial=" + list.size());
		if (list.size() > 0) {
			return list;
		}
		return null;
	}

	public Double getMoneyCount(GmUser guser) {
		System.out.println("getMoneyCount to be used");
		List list = gmOrderDAO.findMoneyCount(guser);
		if (list.size() > 0) {
			// System.out.println("getMoneyCount using");
			// if(list.get(0) instanceof Long){
			// System.out.println("Long");
			// }
			// if(list.get(0) instanceof Double){
			// System.out.println("Double");
			// }
			// if(list.get(0) instanceof Integer){
			// System.out.println("Integer");
			// }
			Double result = (Double) list.get(0);
			System.out.println("getMoneyCount result =" + result);
			return result;
		}
		return 0.0;
	}

	public List findHomeData() {
		List result = new ArrayList();
		List list = gmOrderDAO.findHome();
		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				if (i > 8) {
					break;
				}
				GmOrder gm = (GmOrder) list.get(i);
				result.add(gm);
			}
		}
		System.out.println(result.size());
		return exchangeOrderValues(result);
	}

	public List getAllOrder() {
		List list = gmOrderDAO.findAll();
		if (list.size() > 0) {
			return list;
		}
		return null;
	}

	public List searchOrder(Integer state, String qq, String userName,
			String saler, String orderNum) {
		List list = gmOrderDAO.findOrder(state, qq, userName, saler, orderNum);
		if (list.size() > 0) {
			return list;
		}
		return null;
	}

	public List getBuyerOrder(String username) {
		List list = gmOrderDAO.findBuyerOrder(username);
		if (list.size() > 0) {
			return exchangeOrderValues(list);
		}
		return null;
	}

	public List getSalerOrder(String username) {
		List result = new ArrayList();
		List list1 = gmOrderDAO.findSalerOrder(username);
		if (list1.size() > 0) {
			for (int i = 0; i < list1.size(); i++) {
				GmOrder gm = (GmOrder) list1.get(i);
				result.add(gm);
			}
		}
		List list2 = gmUserDAO.findSonUser(username);
		if (list2.size() > 0) {
			for (int i = 0; i < list2.size(); i++) {
				GmUser g = (GmUser) list2.get(i);
				List list3 = gmOrderDAO.findSalerOrder(g.getUserName());
				if (list3.size() > 0) {
					for (int j = 0; j < list3.size(); j++) {
						GmOrder o = (GmOrder) list3.get(j);
						result.add(o);
					}
				}
			}
		}
		System.out.println(result.size());
		return exchangeOrderValues(result);
	}

	public String getbuyNum() {
		return new DateUtil().toGetOrderNum();
	}

	public String getpayNum() {
		return new DateUtil().toPayMoneyNum();
	}

	public void giveOrderTo(GmUser guser, GmOrder gorder) {
		gorder.setOperator(guser.getUserName());
		gmOrderDAO.saveOrUpdate(gorder);
	}

	public void salerOrderMessege(GmOrder gorder) {
		GmUser user = gorder.getGmGameStore().getGmUser();
		user.setSalerMessagecount(user.getSalerMessagecount() + 1);
		gmUserDAO.saveOrUpdate(user);
	}

	// public List findGoodData(GmUser guser) {
	// List result = new ArrayList();
	// // String userName = guser.getUserName();
	// List list = gmOrderDAO.findGood(guser.getUserName());
	// if (list.size() > 0) {
	// for (int i = 0; i < list.size(); i++) {
	// GmOrder gm = (GmOrder) list.get(i);
	// result.add(gm);
	// }
	// }
	// System.out.println(result.size());
	// return result;
	// }
	public GmOrder buyByBanlance(GmOrder gOrder, GmUser gmUser) {
		System.out.println("buyByBanlance1=" + gmUser.getCountBalance() + "-"
				+ gOrder.getFactMoney() + "-" + gOrder.getNeedMoney() + "-"
				+ gOrder.getPrice() + gOrder.getRemark());
		if (gmUser.getCountBalance() < gOrder.getFactMoney()) {
			return null;
		} else {
			DecimalFormat df = new DecimalFormat("#.##");
			gmUser.setCountBalance(Double.valueOf(df.format(gmUser
					.getCountBalance()
					- gOrder.getFactMoney())));
			System.out.println("buyByBanlance2=" + gmUser.getCountBalance()
					+ "-" + gOrder.getFactMoney() + "-" + gOrder.getNeedMoney()
					+ "-" + gOrder.getPrice() + gOrder.getRemark());
			gmUserDAO.saveOrUpdate(gmUser);
		}

		List list1 = gmGameDAO.findByGameName(gOrder.getGmGameStore()
				.getGmGame().getGameName());
		if (list1.size() > 0) {
			GmGame game = (GmGame) list1.get(0);
			gOrder.setGmGame(game);
		}
		System.out.println("list1 used ");
		List list2 = gmCserverDAO.defineUnit(gOrder.getGmGameStore()
				.getGmGame().getGameName(), gOrder.getGmGameStore()
				.getGmCserver().getCserverName(), gOrder.getGmGameStore()
				.getGmCserver().getSeverType(), gOrder.getGmGameStore()
				.getGmCserver().getUnit(), gOrder.getGmGameStore()
				.getGmCserver().getCamp());

		if (list2.size() > 0) {
			GmCserver gmCserver = (GmCserver) list2.get(0);
			gOrder.setGmCserver(gmCserver);
		}
		System.out.println("list2 used ");
		List list3 = gmGameStoreDAO.defineGameStore(gOrder.getGmGameStore());
		if (list3.size() > 0) {
			GmGameStore gameStore = (GmGameStore) list3.get(0);

			System.out.println("buyByBanlance gameStore1="
					+ gameStore.getStoreNum() + "---" + gOrder.getBuyNum());
			gameStore.setStoreNum(gameStore.getStoreNum() - gOrder.getBuyNum());
			System.out.println("buyByBanlance gameStore2="
					+ gameStore.getStoreNum() + "---" + gOrder.getBuyNum());
			gmGameStoreDAO.saveOrUpdate(gameStore);

			gOrder.setGmGameStore(gameStore);
		}
		System.out.println("list3 used ");
		gOrder.setGmUser(gmUser);
		gOrder.setOrderType(new Integer(3));
		gOrder.setState(new Integer(0));
		gOrder.setPayState(new Integer(1));
		gOrder.setId(null);
		gOrder.setRemark(gOrder.getRemark());
		gOrder.setPrice(gOrder.getGmGameStore().getPrice());
		gOrder.setKfUid(gmUser.getId());
		gOrder.setCreateDate(new DateUtil().toTimeString());
		gOrder.setOrderNum(gOrder.getOrderNum());
		gOrder.setOrderType(new Integer(3));
		gOrder.setPayDate(new DateUtil().toTimeString());
		gOrder.setBuyWay(gOrder.getBuyWay());
		System.out.println("buyByBanlance--" + gOrder.getFactMoney() + "-"
				+ gOrder.getNeedMoney() + "-" + gOrder.getPrice() + "-"
				+ gOrder.getRemark());

		gmOrderDAO.saveOrUpdate(gOrder);
		return gOrder;
	}

	public static Connection getConnectionByJDBC() {		
		return DBPoolFactory.getConnectionByJDBC();
	}
	public int findCserverId(int gameId, String cserverName, String severType,
			String unit, String camp, String danwei) {
		int id = -1;

		String sqlx = "select g.id from gm_cserver g where g.game_id=" + gameId
				+ " and g.cserver_name='" + cserverName
				+ "' and g.sever_type='" + severType + "' and g.unit='" + unit
				+ "' and g.camp='" + camp + "' and g.danwei='" + danwei + "'";
		try {
			Connection conn = getConnectionByJDBC();
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sqlx);
			if (rs.next()) {
				id = rs.getInt("id");

			}
			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		return id;
	}

	public int findUserId(String username) {
		int id = -1;

		String sqlx = "select id from gm_user where user_name='" + username
				+ "'";
		try {
			Connection conn = getConnectionByJDBC();
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sqlx);
			if (rs.next()) {
				id = rs.getInt("id");
			}
			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		return id;
	}

	public int findGameId(String gameName) {
		int id = -1;

		String sqlx = "select id from gm_game where game_name='" + gameName
				+ "'";
		try {
			Connection conn = getConnectionByJDBC();
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sqlx);
			if (rs.next()) {
				id = rs.getInt("id");
			}
			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		return id;
	}

	public void save(String sql) {
		try {
			Connection conn = getConnectionByJDBC();
			Statement stmt = conn.createStatement();
			stmt.execute(sql);
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
	}

	// 用最新的购买方式
	public String buySingle(GmOrder gOrder, GmUser gmUser) {
		int gameId = findGameId(gOrder.getGmGameStore().getGmGame()
				.getGameName());
		if (gameId > 0) {
			int cserverId = findCserverId(gameId, gOrder.getGmGameStore()
					.getGmCserver().getCserverName(), gOrder.getGmGameStore()
					.getGmCserver().getSeverType(), gOrder.getGmGameStore()
					.getGmCserver().getUnit(), gOrder.getGmGameStore()
					.getGmCserver().getCamp(), gOrder.getGmGameStore()
					.getGmCserver().getDanWei());
			if (cserverId > 0) {
				String sql = "";
				int storeid = gOrder.getGmGameStore().getId();

				sql = "insert into gm_order(order_num,user_id,game_id,cserver_id,store_id,buy_num,price,need_money,unit,tel,qq,"
						+ "create_date,pay_state,state,fact_money,game_juese,juese_level,buy_way,remark,kf_uid,order_type,rec_state) ";
				sql = sql + " values('" + gOrder.getOrderNum() + "',"
						+ gmUser.getId() + "," + gameId + "," + cserverId + ","
						+ storeid + "," + gOrder.getBuyNum() + ","
						+ gOrder.getPrice() + "," + gOrder.getNeedMoney() + ",'"
						+ gOrder.getUnit() + "','" + gOrder.getTel() + "','"
						+ gOrder.getQq() + "','" + new DateUtil().toTimeString()
						+ "',0,0," + gOrder.getFactMoney() + ",'"
						+ gOrder.getGameJuese() + "','"
						+ gOrder.getJueseLevel() + "'," + gOrder.getBuyWay()
						+ ",'" + gOrder.getRemark() + "'," + gmUser.getId()
						+ ",3,0)";

				System.out.println("buySingle--" + sql);
				save(sql);
				
			} else {
				return "fail";
			}
		} else {
			return "fail";
		}
		return "success";
	}

	// 原来购买方式
	// public GmOrder buySingle(GmOrder gOrder, GmUser gmUser) {
	// System.out.println("2" + gOrder.getFactMoney() + "-"
	// + gOrder.getNeedMoney() + "-" + gOrder.getPrice()
	// + gOrder.getRemark());
	// List list1 = gmGameDAO.findByGameName(gOrder.getGmGameStore()
	// .getGmGame().getGameName());
	// if (list1.size() > 0) {
	// GmGame game = (GmGame) list1.get(0);
	// gOrder.setGmGame(game);
	// }
	// System.out.println("list1 used ");
	// List list2 = gmCserverDAO.defineUnit(gOrder.getGmGameStore()
	// .getGmGame().getGameName(), gOrder.getGmGameStore()
	// .getGmCserver().getCserverName(), gOrder.getGmGameStore()
	// .getGmCserver().getSeverType(), gOrder.getGmGameStore()
	// .getGmCserver().getUnit(), gOrder.getGmGameStore()
	// .getGmCserver().getCamp());
	//
	// if (list2.size() > 0) {
	// GmCserver gmCserver = (GmCserver) list2.get(0);
	// gOrder.setGmCserver(gmCserver);
	// }
	// System.out.println("list2 used ");
	// List list3 = gmGameStoreDAO.defineGameStore(gOrder.getGmGameStore());
	// if (list3.size() > 0) {
	// GmGameStore gameStore = (GmGameStore) list3.get(0);
	// gOrder.setGmGameStore(gameStore);
	// }
	// System.out.println("list3 used ");
	// gOrder.setGmUser(gmUser);
	// gOrder.setOrderType(new Integer(3));
	// gOrder.setState(new Integer(0));
	// gOrder.setPayState(new Integer(0));
	// gOrder.setId(null);
	// gOrder.setRemark(gOrder.getRemark());
	// gOrder.setPrice(gOrder.getGmGameStore().getPrice());
	// gOrder.setKfUid(gmUser.getId());
	// gOrder.setCreateDate(new DateUtil().toTimeString());
	// gOrder.setOrderNum(gOrder.getOrderNum());
	// gOrder.setOrderType(new Integer(3));
	// gOrder.setBuyWay(gOrder.getBuyWay());
	// System.out.println("buySingle--" + gOrder.getFactMoney() + "-"
	// + gOrder.getNeedMoney() + "-" + gOrder.getPrice()
	// + gOrder.getRemark());
	//
	// gmOrderDAO.saveOrUpdate(gOrder);
	// return gOrder;
	// }
	public GmOrder resumeBuy(GmOrder temp, GmUser gmUser) {
		GmOrder gOrder = gmOrderDAO.findById(temp.getId());
		if (gmUser.getCountBalance() < gOrder.getFactMoney()) {
			return null;
		} else {
			DecimalFormat df = new DecimalFormat("#.##");
			gmUser.setCountBalance(Double.valueOf(df.format(gmUser
					.getCountBalance()
					- gOrder.getFactMoney())));
			System.out.println("resumeBuy3=" + gmUser.getCountBalance() + "-"
					+ gOrder.getFactMoney() + "-" + gOrder.getNeedMoney() + "-"
					+ gOrder.getPrice() + gOrder.getRemark());
			gmUserDAO.saveOrUpdate(gmUser);
		}
		System.out.println("resumeBuy2" + gOrder.getFactMoney() + "-"
				+ gOrder.getNeedMoney() + "-" + gOrder.getPrice() + "-"
				+ gOrder.getRemark());

		GmGameStore gameStore = (GmGameStore) gOrder.getGmGameStore();
		System.out.println("resumeBuy gameStore1=" + gameStore.getStoreNum()
				+ "---" + gOrder.getBuyNum());
		gameStore.setStoreNum(gameStore.getStoreNum() - gOrder.getBuyNum());
		System.out.println("resumeBuy gameStore2=" + gameStore.getStoreNum()
				+ "---" + gOrder.getBuyNum());
		gmGameStoreDAO.saveOrUpdate(gameStore);

		gOrder.setPayState(new Integer(1));
		gOrder.setPayDate(new DateUtil().toTimeString());
		System.out.println("resumeBuy1--" + gOrder.getFactMoney() + "-"
				+ gOrder.getNeedMoney() + "-" + gOrder.getPrice()
				+ gOrder.getRemark());
		gmOrderDAO.saveOrUpdate(gOrder);
		return gOrder;
	}

	public List checkOutByBanlance(List orders, GmUser gmUser, double fee) {
		try {
			System.out.println("checkOutByBanlance-buyByBanlance1="
					+ gmUser.getCountBalance() + "-" + fee + "-");
			if (gmUser.getCountBalance() < fee) {
				return null;
			} else {
				DecimalFormat df = new DecimalFormat("#.##");
				gmUser.setCountBalance(Double.valueOf(df.format(gmUser
						.getCountBalance()
						- fee)));
				gmUserDAO.saveOrUpdate(gmUser);
			}

			System.out.println("checkOutByBanlance to be used ");
			if (orders.size() > 0) {
				System.out.println("orders.size() > 0 " + orders.size());
				List result = new ArrayList();
				for (int i = 0; i < orders.size(); i++) {
					System.out.println("checkOut using ");
					GmOrder gOrder = (GmOrder) orders.get(i);
					List list1 = gmGameDAO.findByGameName(gOrder
							.getGmGameStore().getGmGame().getGameName());
					if (list1.size() > 0) {
						GmGame game = (GmGame) list1.get(0);
						gOrder.setGmGame(game);
					}
					System.out.println("list1 used ");

					List list2 = gmCserverDAO.defineUnit(gOrder
							.getGmGameStore().getGmGame().getGameName(), gOrder
							.getGmGameStore().getGmCserver().getCserverName(),
							gOrder.getGmGameStore().getGmCserver()
									.getSeverType(), gOrder.getGmGameStore()
									.getGmCserver().getUnit(), gOrder
									.getGmGameStore().getGmCserver().getCamp());

					if (list2.size() > 0) {
						GmCserver gmCserver = (GmCserver) list2.get(0);
						gOrder.setGmCserver(gmCserver);
					}
					System.out.println("list2 used ");
					List list3 = gmGameStoreDAO.defineGameStore(gOrder
							.getGmGameStore());
					if (list3.size() > 0) {
						GmGameStore gameStore = (GmGameStore) list3.get(0);

						System.out.println("checkOutByBanlance gameStore1="
								+ gameStore.getStoreNum() + "---"
								+ gOrder.getBuyNum());
						gameStore.setStoreNum(gameStore.getStoreNum()
								- gOrder.getBuyNum());
						System.out.println("checkOutByBanlance gameStore2="
								+ gameStore.getStoreNum() + "---"
								+ gOrder.getBuyNum());
						gmGameStoreDAO.saveOrUpdate(gameStore);

						gOrder.setGmGameStore(gameStore);
					}
					System.out.println("list3 used ");
					gOrder.setGmUser(gmUser);
					gOrder.setOrderType(new Integer(3));
					gOrder.setState(new Integer(0));
					gOrder.setPayState(new Integer(1));
					gOrder.setPayDate(new DateUtil().toTimeString());
					gOrder.setId(null);
					gOrder.setRemark(gOrder.getRemark());
					gOrder.setPrice(gOrder.getGmGameStore().getPrice());
					gOrder.setKfUid(gmUser.getId());
					gOrder.setCreateDate(new DateUtil().toTimeString());
					gOrder.setOrderNum(new DateUtil().toGetOrderNum());
					gOrder.setOrderType(new Integer(3));
					gOrder.setBuyWay(gOrder.getBuyWay());

					System.out.println(new DateUtil().toGetOrderNum());
					System.out.println("checkOutByBanlance-"
							+ gOrder.getFactMoney() + "-"
							+ gOrder.getNeedMoney() + "-" + gOrder.getPrice());

					gmOrderDAO.saveOrUpdate(gOrder);
					System.out.println("orders count = " + i);
					result.add(gOrder);
				}
				return result;
			}
			return null;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public List checkOut(List orders, GmUser gmUser) {
		try {
			System.out.println("checkOut to be used ");
			if (orders.size() > 0) {
				System.out.println("orders.size() > 0 " + orders.size());
				List result = new ArrayList();
				for (int i = 0; i < orders.size(); i++) {
					System.out.println("checkOut using ");
					GmOrder gOrder = (GmOrder) orders.get(i);
					List list1 = gmGameDAO.findByGameName(gOrder
							.getGmGameStore().getGmGame().getGameName());
					if (list1.size() > 0) {
						GmGame game = (GmGame) list1.get(0);
						gOrder.setGmGame(game);
					}
					System.out.println("list1 used ");

					List list2 = gmCserverDAO.defineUnit(gOrder
							.getGmGameStore().getGmGame().getGameName(), gOrder
							.getGmGameStore().getGmCserver().getCserverName(),
							gOrder.getGmGameStore().getGmCserver()
									.getSeverType(), gOrder.getGmGameStore()
									.getGmCserver().getUnit(), gOrder
									.getGmGameStore().getGmCserver().getCamp());

					if (list2.size() > 0) {
						GmCserver gmCserver = (GmCserver) list2.get(0);
						gOrder.setGmCserver(gmCserver);
					}
					System.out.println("list2 used ");
					List list3 = gmGameStoreDAO.defineGameStore(gOrder
							.getGmGameStore());
					if (list3.size() > 0) {
						GmGameStore gameStore = (GmGameStore) list3.get(0);
						gOrder.setGmGameStore(gameStore);
					}
					System.out.println("list3 used ");
					gOrder.setGmUser(gmUser);
					gOrder.setOrderType(new Integer(3));
					gOrder.setState(new Integer(0));
					gOrder.setPayState(new Integer(0));
					gOrder.setId(null);
					gOrder.setRemark(gOrder.getRemark());
					gOrder.setPrice(gOrder.getGmGameStore().getPrice());
					gOrder.setKfUid(gmUser.getId());
					gOrder.setCreateDate(new DateUtil().toTimeString());
					gOrder.setOrderNum(new DateUtil().toGetOrderNum());
					gOrder.setOrderType(new Integer(3));
					gOrder.setBuyWay(gOrder.getBuyWay());
					System.out.println(new DateUtil().toGetOrderNum());
					System.out.println("checkOut-" + gOrder.getFactMoney()
							+ "-" + gOrder.getNeedMoney() + "-"
							+ gOrder.getPrice());

					gmOrderDAO.saveOrUpdate(gOrder);
					System.out.println("orders count = " + i);
					result.add(gOrder);
				}
				return result;
			}
			return null;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public List getOrderDetial(GmOrder gorder) throws Exception {
		try {

			List list = gmOrderDAO.findOrderDetial(gorder);

			System.out.println(list.size());
			return exchangeOrderValues(list);
		} catch (Exception re) {
			re.printStackTrace();
			throw re;
		}

	}

	public List getSalerOrderDetial(GmUser guser, GmOrder gorder) {

		List list1 = gmOrderDAO.findSalerOrderDetial(guser.getUserName(),
				gorder);
		if (list1.size() > 0) {
			System.out.println("getSalerOrderDetial=" + list1.size());
			return exchangeOrderValues(list1);
		}
		// List list2 = gmUserDAO.findSonUserList(guser);
		// if(list2.size()>0){
		// for(int i=0;i<list2.size();i++){
		// GmUser us = (GmUser) list2.get(i);
		// List list3 =
		// gmOrderDAO.findSalerOrderDetial(us.getUserName(),gorder);
		// if(list3.size()>0){
		// for(int j=0;j<list3.size();j++){
		// GmOrder go = (GmOrder) list3.get(i);
		// result.add(go);
		// }
		// }
		// }
		// }

		return new ArrayList();
	}

	public int findNewBuyOrder(GmUser guser) {
		List list = gmOrderDAO.getNewBuyOrder(guser.getUserName());
		if (list.size() > 0) {

			System.out.println("findNewBuyOrder=" + list.size());
			return list.size();
		}
		return 0;
	}

	public List findNewBuyOrders(GmUser guser) {
		List list = gmOrderDAO.getNewBuyOrder(guser.getUserName());
		if (list.size() > 0) {
			return list;
		}
		return null;
	}

	public int findNewSaleOrder(GmUser guser) {
		List list = gmOrderDAO.getNewSaleOrder(guser.getUserName());
		if (list.size() > 0) {
			System.out.println("findNewSaleOrder=" + list.size());
			return list.size();
		}
		return 0;
	}

	public List findNewSaleOrders(GmUser guser) {
		List list = gmOrderDAO.getNewSaleOrder(guser.getUserName());
		if (list.size() > 0) {
			return list;
		}
		return null;
	}

	public List getFundDetial(GmOrder gorder) {
		List result = new ArrayList();
		List list = gmOrderDAO.findFundDetial(gorder);
		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				GmOrder gm = (GmOrder) list.get(i);
				result.add(gm);
			}
		}
		System.out.println(result.size());
		return exchangeOrderValues(result);
	}

	public List getHistoryOrder(GmOrder gorder) {
		// List result = new ArrayList();
		List list = gmOrderDAO.findHistoryOrder(gorder);

		// if (list.size() > 0) {
		// for (int i = 0; i < list.size(); i++) {
		// GmOrder gm = (GmOrder) list.get(i);
		// result.add(gm);
		// }
		// }
		System.out.println(gorder.getGmUser().getId()
				+ gorder.getGmUser().getUserName() + gorder.getCreateDate()
				+ gorder.getPayDate());
		System.out.println(list.size());
		return exchangeOrderValues(list);
	}

	public List getQueryData(String orderNum) {
		List result = new ArrayList();
		List list = gmOrderDAO.findQuery(orderNum);
		if (list.size() > 0) {
			GmOrder gm = (GmOrder) list.get(0);
			result.add(gm);
		}
		System.out.println(result.size());
		return result;
	}

	private List switchOrderValues(List list) {
		List result = new ArrayList();
		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				GmOrder gm = (GmOrder) list.get(i);
				switch (gm.getState().intValue()) {
				// 0未处理1已处理2异常取消3已发货���
				case 0:
					gm.setStateValue("未处理");
					break;
				case 1:
					gm.setStateValue("已处理");
					break;
				case 2:
					gm.setStateValue("异常取消");
					break;
				case 3:
					gm.setStateValue("已发货");
					break;
				}
				switch (gm.getPayState().intValue()) {
				// 0未付款1已付�
				case 0:
					gm.setPayStateValue("未付款");
					break;
				case 1:
					gm.setPayStateValue("已付");
					break;
				}
				result.add(gm);
				gm = null;
			}
		}

		return result;
	}

	private List exchangeOrderValues(List list) {
		List result = new ArrayList();
		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				GmOrder gm = (GmOrder) list.get(i);
				switch (gm.getOrderType().intValue()) {
				// 1充值2提现3支付4售得5退款��˿�
				case 1:
					gm.setOrderTypeValue("充值");
					break;
				case 2:
					gm.setOrderTypeValue("提现");
					break;
				case 3:
					gm.setOrderTypeValue("支付");
					break;
				case 4:
					gm.setOrderTypeValue("售得");
					break;
				case 5:
					gm.setOrderTypeValue("退款");
					break;
				}
				switch (gm.getGmUser().getPaytype().intValue()) {
				// 1支付宝2财付通3网站余额��վ���
				case 1:
					gm.getGmUser().setPaytypeValue("支付宝");
					break;
				case 2:
					gm.getGmUser().setPaytypeValue("财付通");
					break;
				case 3:
					gm.getGmUser().setPaytypeValue("网站余额");
					break;
				}
				switch (gm.getPayState().intValue()) {
				// 0未付款1已付�
				case 0:
					gm.setPayStateValue("未付款");
					break;
				case 1:
					gm.setPayStateValue("已付");
					break;
				}
				switch (gm.getState().intValue()) {
				// 0未处理1已处理2异常取消3已发货���
				case 0:
					gm.setStateValue("未处理");
					break;
				case 1:
					gm.setStateValue("已处理");
					break;
				case 2:
					gm.setStateValue("异常取消");
					break;
				case 3:
					gm.setStateValue("已发货");
					break;
				}
				switch (gm.getBuyWay().intValue()) {
				// 1邮寄2当面交易��
				case 1:
					gm.setBuyWayValue("邮寄");
					break;
				case 2:
					gm.setBuyWayValue("当面交易");
					break;
				}
				result.add(gm);
				gm = null;
			}
		}

		return result;
	}

	public GmOrder saveOrderStateGiveBack(GmOrder temp) {
		GmOrder order = (GmOrder) gmOrderDAO.findById(temp.getId());
		if (order != null) {
			GmGameStore gameStore = order.getGmGameStore();
			System.out.println("saveOrderStateGiveBack gameStore1="
					+ gameStore.getStoreNum() + "---" + order.getBuyNum());
			gameStore.setStoreNum(gameStore.getStoreNum() + order.getBuyNum());
			System.out.println("saveOrderStateGiveBack gameStore2="
					+ gameStore.getStoreNum() + "---" + order.getBuyNum());
			gmGameStoreDAO.saveOrUpdate(gameStore);
			System.out.println("saveOrderStateGiveBack getPayState="
					+ order.getOrderNum() + "---" + temp.getPayState());

			order.setPayState(temp.getPayState());
			order.setState(temp.getState());
			gmOrderDAO.saveOrUpdate(order);

			return order;
		}
		return null;
	}

	public void savePayed(GmOrder temp) {
		GmOrder order = (GmOrder) gmOrderDAO.findById(temp.getId());
		if (order != null) {
			order.setPayState(temp.getPayState());
			order.setPayDate(new DateUtil().toTimeString());
			gmOrderDAO.saveOrUpdate(order);
		}
	}

	public void saveOrderState(GmOrder temp) {
		GmOrder order = (GmOrder) gmOrderDAO.findById(temp.getId());
		if (order != null) {
			order.setState(temp.getState());
			gmOrderDAO.saveOrUpdate(order);
		}
	}

	public void saveSalerOrderState(GmOrder temp) {
		GmOrder order = (GmOrder) gmOrderDAO.findById(temp.getId());
		if (order != null) {
			order.setState(temp.getState());
			order.setSendDate(new DateUtil().toTimeString());
			gmOrderDAO.saveOrUpdate(order);
		}
	}

	public void saveBuyOrderState(GmOrder temp) {
		GmOrder order = (GmOrder) gmOrderDAO.findById(temp.getId());
		if (order != null) {
			order.setRecState(temp.getRecState());
			order.setRecDate(new DateUtil().toTimeString());
			gmOrderDAO.saveOrUpdate(order);
		}
	}

	// public void addNewGood(GmOrder cashDrawOrder){
	//
	//
	// GmOrder u = new GmOrder();
	// u.getGmUser().setPaytype(cashDrawOrder.getGmUser().getPaytype());
	// u.setFactMoney(cashDrawOrder.getFactMoney());
	// u.getGmUser().setUserName(cashDrawOrder.getGmUser().getUserName());
	// u.setOrderType(new Integer(2));
	// u.setCreateDate(new DateUtil().toDateString());
	// u.setOrderNum(new DateUtil().toGetOrderNum());
	// System.out.println(new DateUtil().toGetOrderNum());
	// gmOrderDAO.saveOrUpdate(u);
	// }

	public void setGmGameDAO(GmGameDAO gmGameDAO) {
		this.gmGameDAO = gmGameDAO;
	}

	public GmGameDAO getGmGameDAO() {
		return gmGameDAO;
	}

	public void setGmCserverDAO(GmCserverDAO gmCserverDAO) {
		this.gmCserverDAO = gmCserverDAO;
	}

	public GmCserverDAO getGmCserverDAO() {
		return gmCserverDAO;
	}

	public void setGmGameStoreDAO(GmGameStoreDAO gmGameStoreDAO) {
		this.gmGameStoreDAO = gmGameStoreDAO;
	}

	public GmGameStoreDAO getGmGameStoreDAO() {
		return gmGameStoreDAO;
	}

}
